/**
 * Created by liubin on 2017/5/10.
 */


var DzpkCardTypeTipLayer  = GameCCSBase.extend({

    Panel_main : null,

    Panel_cardType : null,

    Panel_shangju : null,

    CheckBox_cardType : null,

    CheckBox_shangju : null,

    publicCards : null,

    playerCards : null,

    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/DzpkCardTypeTipLayer.json");

        this.loadUI();
    },

    loadUI : function () {
        this.Panel_main = mo.seekWidgetByName(this.ccsNode, "Panel_main");
        this.Panel_main.setVisible(false);
        this.Panel_main.setPosition(cc.p(-mo.gameSize.x/2, 0));
        this.Panel_main.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);

        this.Panel_cardType = mo.seekWidgetByName(this.ccsNode, "Panel_cardType");

        this.Panel_shangju = mo.seekWidgetByName(this.ccsNode, "Panel_shangju");

        this.CheckBox_shangju = mo.seekWidgetByName(this.ccsNode, "Image_CheckBox_1");

        this.CheckBox_cardType = mo.seekWidgetByName(this.ccsNode, "Image_CheckBox_2");

        this.CheckBox_shangju.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {

                mo.gameHelper.loadTextureImage(this.CheckBox_shangju, "res/ccs/dzpk/card_typecard-type-tab.png");
                mo.gameHelper.loadTextureImage(this.CheckBox_cardType, "res/ccs/dzpk/card_typecard-type-tab2.png");

                this.Panel_shangju.setVisible(true);
                this.Panel_cardType.setVisible(false);
            }
        }, this);

        this.CheckBox_cardType.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {

                mo.gameHelper.loadTextureImage(this.CheckBox_cardType, "res/ccs/dzpk/card_typecard-type-tab.png");
                mo.gameHelper.loadTextureImage(this.CheckBox_shangju, "res/ccs/dzpk/card_typecard-type-tab2.png");

                this.Panel_cardType.setVisible(true);
                this.Panel_shangju.setVisible(false);
            }
        }, this);

        this.publicCards = [];
        var Panel_gongpai = mo.seekWidgetByName(this.ccsNode, "Panel_gongpai");
        var pos_x = 0;
        for (var i=0; i<5; i++){
            var card = new DzpkCard();
            card.setCardNum(0);
            card.setCardKind(2);
            var offsetX = 52;
            card.setPosition(cc.p(pos_x, 0));
            card.setScale(0.6);
            Panel_gongpai.addChild(card);

            this.publicCards.push(card);

            if(i == 2){
                offsetX = 60;
            }
            pos_x += offsetX;
        }

        this.playerCards = [];
        for (var i=0; i<5; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "player" + i);
            var Panel_cards = mo.seekWidgetByName(player, "Panel_cards");

            for (var j=0; j<2; j++){
                var card = new DzpkCard();
                card.setCardNum(0);
                card.setCardKind(2);
                card.setPosition(cc.p(j*55, 0));
                card.setScale(0.6);
                Panel_cards.addChild(card);

                this.playerCards.push(card);
            }

            player.setVisible(false);
        }


    },

    setDialog : function (state) {
        this.Panel_main.stopAllActions();
        if(state){
            this.Panel_main.runAction(cc.sequence(
                cc.show(),
                cc.moveTo(0.2, 0, 0)
            ));
        }else{
            this.Panel_main.runAction(cc.sequence(
                cc.moveTo(0.2, -mo.gameSize.x/2, 0),
                cc.hide()
            ));
        }
    },

    setLastRoundInfo : function (LastRoundEndResponsedzpk) {
        if(LastRoundEndResponsedzpk == null){
            return;
        }

        var endInfo = LastRoundEndResponsedzpk.re;

        var publicCardNums = [];
        var seatUserArray = [];

        for (var i = 0; i < 5; i++) {
            var logicSeat = mo.dzpkTableStatus.getLogicForView(i, endInfo.MySeat - 1);
            var seatUser = endInfo["SeatUser" + (logicSeat + 1)];
            if (seatUser != null){
                seatUserArray.push(seatUser);

                if(seatUser.playState){
                    if(seatUser.HandCards.length == 7 && publicCardNums.length == 0){
                        for (var index=0; index<5; index++){
                            publicCardNums.push(seatUser.HandCards[index+2]);
                        }
                    }
                }
            }
        }

        for (var i=0; i<5; i++){
            this.publicCards[i].setCardNum(publicCardNums[i]);
            this.publicCards[i].setCardKind(2);
        }

        for (var i=0; i<5; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "player" + i);
            player.setVisible(true);
            if(i < seatUserArray.length){
                var seatUser = seatUserArray[i];
                if(seatUser != null){
                    //名字
                    var Text_name = mo.seekWidgetByName(player, "Text_name");
                    mo.gameHelper.setTextString(Text_name, seatUser.user_info.nickname);

                    //庄家
                    var Image_host = mo.seekWidgetByName(player, "Image_host");
                    Image_host.setVisible(seatUser.IsHost);

                    //上局结果
                    var Text_fen = mo.seekWidgetByName(player, "Text_fen");

                    var allfen = 0;
                    var ids = seatUser.LostWin.ItemTypes;
                    var nums = seatUser.LostWin.ItemNums;
                    allfen = mo.gameUserInfo.getItemNum(ids, nums, 0);
                    if(allfen >= 0){
                        allfen = "+" + allfen;
                    }
                    mo.gameHelper.setTextString(Text_fen, allfen);

                    //牌型
                    var Text_cardType = mo.seekWidgetByName(player, "Text_cardType");
                    var cardTypeStr = mo.dzpkCardHelper.getCardTypeCHS(seatUser.Cardtype)
                    mo.gameHelper.setTextString(Text_cardType, cardTypeStr);

                    var HandCards = seatUser.HandCards;

                    if(seatUser.playState == false){
                        mo.gameHelper.setTextString(Text_cardType, "弃牌");
                        HandCards = [0, 0];
                    }

                    this.playerCards[i*2].setCardNum(HandCards[0]);
                    this.playerCards[i*2].setCardKind(2);

                    this.playerCards[i*2+1].setCardNum(HandCards[1]);
                    this.playerCards[i*2+1].setCardKind(2);


                }
            }else{
                player.setVisible(false);
            }
        }
    },

})